package com.rh.bmw4s.view.fragdialog;

import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.rh.bmw4s.R;
import com.rh.bmw4s.adapter.FianlAdapter;
import com.rh.bmw4s.bean.ThirdSearchBean;
import com.rh.bmw4s.comm.ConfigValue;
import com.rh.bmw4s.comm.Constant;
import com.rh.bmw4s.utills.LogUtils;
import com.rh.bmw4s.utills.SPUtils;
import com.rh.bmw4s.utills.ScreenUtils;
import com.rh.bmw4s.utills.SizeUtils;
import com.rh.bmw4s.utills.ToastUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * 回收二手车界面
 * 选择车辆颜色
 * 选择车辆过户次数
 */

public class SelectColorDialog extends DialogFragment implements View.OnClickListener {

    private TextView tvCancel, tvTitle;
    private TagFlowLayout flowSelect;
    private List<String> strDatas;
    private int itemWidth = 0;

    private int type; //1颜色  2 过户次数

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public void onStart() {
        super.onStart();
        //获取屏幕信息
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;//ScreenUtils.getScreenWidth()
        // params.height = WindowManager.LayoutParams.MATCH_PARENT;//ScreenUtils.getScreenHeight()
        params.gravity = Gravity.RIGHT | Gravity.BOTTOM;
        window.setAttributes(params);
//        window.setGravity(Gravity.CENTER);
//        //这里用屏幕高度，不覆盖状态栏
        //window.setLayout((int) (metrics.widthPixels / 2), ScreenUtils.getScreenHeight());
        //设置布局部分的背景色
        window.setBackgroundDrawable(new ColorDrawable(000000));
        //整体背景色(设置透明度)
        window.setDimAmount(0.5f);
        //动画
        //window.setWindowAnimations(R.style.BottmoDialogAnim2);
        getDialog().setCanceledOnTouchOutside(false);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.custom_select_frag, container);


        //动态获取item宽度
        int width = ScreenUtils.getScreenWidth();
        itemWidth = (int) (width - SizeUtils.dp2px(50f)) / 4;

        tvCancel = view.findViewById(R.id.tv_select_cancel);
        tvTitle = view.findViewById(R.id.tv_select_kind);
        flowSelect = view.findViewById(R.id.flow_select_carparams);
        flowSelect.setMaxSelectCount(1);
        tvCancel.setOnClickListener(this);

        if (type == 1) {
            tvTitle.setText("选择颜色");
            getColorData();
        } else {
            tvTitle.setText("过户次数");
            getTimesData();
        }

        getSelect();
        return view;
    }

    private void getTimesData() {
        strDatas = new ArrayList<>();
        strDatas.add("0次");
        strDatas.add("1次");
        strDatas.add("2次");
        strDatas.add("3次");
        strDatas.add("4次");
        strDatas.add("5次");
        strDatas.add("6次");
        strDatas.add("7次");
        strDatas.add("8次");
        strDatas.add("9次");
        strDatas.add("10次");
        strDatas.add("10次以上");

    }

    private void getColorData() {
        strDatas = new ArrayList<>();
        strDatas.add("米色");
        strDatas.add("白色");
        strDatas.add("灰色");
        strDatas.add("红色");
        strDatas.add("棕色");
        strDatas.add("蓝色");
        strDatas.add("黄色");
        strDatas.add("紫色");
        strDatas.add("黑色");
        strDatas.add("橙色");
        strDatas.add("银色");
        strDatas.add("金色");
        strDatas.add("绿色");
    }


    /**
     * 获取排量信息
     */
    public void getSelect() {
        TagAdapter<String> tagAdapter = new TagAdapter<String>(strDatas) {
            @Override
            public View getView(FlowLayout parent, int position, String tip) {
                TextView textView = (TextView) LayoutInflater.from(getActivity())
                        .inflate(R.layout.item_select_carparams, flowSelect, false);
                textView.setText(tip);
                textView.getLayoutParams().width = itemWidth;

                return textView;
            }

            @Override
            public void unSelected(int position, View view) {
                super.unSelected(position, view);
            }

            @Override
            public void onSelected(int position, View view) {
                super.onSelected(position, view);
            }

            @Override
            public boolean setSelected(int position, String bean) {
                return super.setSelected(position, bean);
            }
        };
        //添加热门标签数据
        flowSelect.setAdapter(tagAdapter);
        //热门标签点击事件
        flowSelect.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (selectItemClickListener != null) {
                    selectItemClickListener.pullSelect(strDatas.get(position));

                }
                return false;
            }
        });

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_select_cancel:
                getDialog().dismiss();
                break;
        }
    }


    //定义接口
    public interface OnSelectItemClickListener {
        void pullSelect(String s);
    }

    private OnSelectItemClickListener selectItemClickListener;

    public void setSelectItemClickListener(OnSelectItemClickListener selectItemClickListener) {
        this.selectItemClickListener = selectItemClickListener;
    }
}
